package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
	@Autowired
	private IDepartmentService departmentService;


	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	@RequiresPermissions(value = {"department:saveOrUpdate","部门添加/修改"},logical = Logical.OR)// 把逻辑改为OR
	@RequiresRoles("admin")
	public JsonResult saveOrUpdate(Department department){
		if(department.getId() != null){
			departmentService.update(department);
		}else {
			departmentService.insert(department);
		}
		return new JsonResult();
	}

	@RequestMapping("/delete")
	@RequiresRoles("admin")
	@ResponseBody
	@RequiresPermissions(value = {"department:delete","部门删除"},logical = Logical.OR)
	public JsonResult delete(Long id){
		if(id != null){
			departmentService.delete(id);
		}
		return new JsonResult();
	}

	@RequestMapping("/list")
	@RequiresPermissions(value = {"department:list","部门页面"},logical = Logical.OR)
	public String list(@ModelAttribute("qo") QueryObject qo, Model model){
		PageInfo<Department> pageInfo = departmentService.query(qo);
		model.addAttribute("pageInfo",pageInfo);
		return "/department/list";
	}
}
